package Server.impl;
import BaseQuery.BaseQuery;
import Bean.Address;
import Dao.IAddressDao;
import Dao.impl.AddressDao;
import Server.IAddressServe;
import com.alibaba.druid.util.StringUtils;
import vo.PageBean;
import vo.ResultBean;

import java.util.List;

public class AddressService implements IAddressServe {

    private IAddressDao addressDao;

    public AddressService()
    {
        addressDao = new AddressDao();
    }

    @Override
    public ResultBean add(Address address) throws Exception {
        //参数校验
        if(address==null ||
                StringUtils.isEmpty(address.getContactName())) {
            return ResultBean.ERROR(1001,"params is null");
        }
        int add = addressDao.add(address);
        return add>0?ResultBean.SUCCESS(address):ResultBean.ERROR("add address is failed");
    }

    @Override
    public ResultBean update(Address address) throws Exception {

        //参数校验
        if(address==null) {
            return ResultBean.ERROR(1001,"params is null");
        }
        int update = addressDao.update(address);
        return update>0?ResultBean.SUCCESS(address):ResultBean.ERROR("update address is failed");
    }

    @Override
    public ResultBean delete(Integer id) throws Exception {
        if(id == null){
            return ResultBean.ERROR(1001,"params is null");
        }
        int delete = addressDao.delete(id);
        return delete>0?ResultBean.SUCCESS():ResultBean.ERROR("delete address is failed");
    }

    @Override
    public ResultBean getById(Integer id) throws Exception {
        if(id == null){
            return ResultBean.ERROR(1001,"params is null");
        }
        Address address = addressDao.getById(id);
        return address!=null?ResultBean.SUCCESS(address):ResultBean.ERROR("get address by id is failed");
    }

    @Override
    public ResultBean getAll() throws Exception {
        List<Address> addressList = addressDao.getAll();
        return addressList!=null?ResultBean.SUCCESS(addressList):ResultBean.ERROR("get address is failed");
    }

    @Override
    public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
        if(baseQuery == null){
            baseQuery = new BaseQuery();
        }
        PageBean<Address> pageBean = new PageBean<>();

        int count = addressDao.getCount(baseQuery);
        pageBean.setTotalRows(count);
        if(count%baseQuery.getPageCount()==0){
            pageBean.setTotalPages(count/baseQuery.getPageCount());
        }else{
            pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
        }
        List<Address> addressList = addressDao.getByPage(baseQuery);
        pageBean.setData(addressList);
//        System.out.println(addressList);
        return ResultBean.SUCCESS(pageBean);
    }
    @Override
    public ResultBean batchDel(List<Integer> ids) throws Exception {
        if(ids==null|| ids.size()==0) {
            return ResultBean.ERROR("params is null");
        }
        for(Integer id:ids) {
            addressDao.delete(id);
        }
        return ResultBean.SUCCESS();
    }
}
